package com.chen.controller;

import com.chen.dto.LogInAndRegisterDto;
import com.chen.service.UserService;
import com.chen.utils.UserThreadLocal;
import com.chen.vo.Result;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author cws
 * @since
 */
@RestController
@RequestMapping
public class LoginAndRegisterController {

    @Resource
    UserService userService;

    @Resource
    private RedisTemplate redisTemplate;


    @PostMapping("/login")
    public Result login(@RequestBody LogInAndRegisterDto logInAndRegisterDto) {
        return userService.login(logInAndRegisterDto);
    }

    @PostMapping("/register")
    public Result register(@RequestBody LogInAndRegisterDto logInAndRegisterDto) {
        return userService.register(logInAndRegisterDto);
    }


    @PostMapping("/logOut")
    public Result logOut(@RequestHeader("Authorization") String token) {
        String loginKey = "TOKEN_" + token;
        String UserKey = "TOKEN:" + token;
        redisTemplate.delete(Arrays.asList(loginKey, UserKey));
        return Result.success();
    }


}

